import axios from 'axios';
axios.defaults.baseURL = 'http://klxin.cn:8888/api/private/v1/';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
//设置axios请求拦截，携带请求头      
//添加请求拦截器
axios.interceptors.request.use(
  function (config) {
    //开启进度条
    NProgress.start();
    //从congig获取token
    config.headers.Authorization = window.sessionStorage.getItem('token');
    return config;
  },
  function (error) {
    //请求失败
    return Promise.reject(error);
  }
);
// 添加响应拦截器---------------------------------------------------------------------
axios.interceptors.response.use(function (response) {
  //关闭进度条
  NProgress.done();
  // 对响应数据做点什么
  // console.log(response, 77777);
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

//1、发送登录请求---------------------------------
export var login_api = (params) => {
  return axios.post('login', params);
};
//2、 菜单导航----------------------------------
export var menus_api = () => {
  return axios.get('menus');
};
//3、获取用户列表-------------------------------
export var users_api = (params) => {
  return axios.get('users', {
    params: params
  });
};
//4、修改用户状态---------------------------------
export var userState_api = (params) => {
  return axios.put(`users/${params.uld}/state/${params.type}`);
};
//5、删除用户信息---------------------------------
export var delUsers_api = (params) => {
  return axios.delete(`users/${params}`);
};
//6、获取角色列表---------------------------------
export var rolesList_api = () => {
  return axios.get('roles');
};
//7、修改用户信息 （邮箱、手机号）------------------------------
export var editUsers_api = (params) => {
  return axios.put('users/' + params.id, {
    email: params.email,
    mobile: params.mobile
  });
};
//8、获取所有权限列表---------------------------------------
export var rigthsList_api = (type) => {
  return axios.get('rights/' + type);
};
//9、添加用户信息---------------------------------------
export var addUsers_api = (params) => {
  return axios.post('users/', params);
};
//10、更改角色权限----------------------------------
export var changeRolesRights_api = (params) => {
  return axios.post(`roles/${params.roleId}/rights`, {
    rids: params.rids
  });
};
//11、分配用户角色----------------------------------------
export var allotRoles_api = (params) => {
  return axios.put(`users/${params.id}/role`);
};
//12、获取商品分类数据列表
export var getCategories_api = (params) => {
  return axios.get('categories', {
    params: {
      type: params.type,
      pagenum: params.pagenum,
      pagesize: params.pagesize
    }
  });
};
//14、编辑提交角色--------------------------------
export var editRoles_api = (params) => {
  return axios.put(`roles/${params.id}`, {
    roleName: params.roleName,
    roleDesc: params.roleDesc
  });
};
//15、删除角色--------------------------------------
export var delRoles_api = (id) => {
  return axios.delete(`roles/${id}`);
};
//16、添加角色----------------------
export var addRoles_api = (params) => {
  return axios.post('roles', {
    roleName: params.roleName,
    roleDesc: params.roleDesc
  });
};
//17、添加分类---------------------------------------
export var addCategories_api = (params) => {
  return axios.post('categories', {
    cat_pid: params.cat_pid,
    cat_name: params.cat_name,
    cat_level: params.cat_level
  });
};
//18、分类参数管理===>>>> 根据 ID 查询参数--------------
//attr_sel ==>>>[only,many]  2选1
export var searchCateAttr_api = (params1) => {
  return axios.get(`categories/${params1.id}/attributes/${params1.attrId}`, {
    params: {
      attr_sel: params1.attr_sel,
    }
  });
};
//19、分类参数管理===>>>>获取参数类表---------------------------
export var getCateList_api = (params1) => {
  return axios.get(`categories/${params1.id}/attributes`, {
    params: {
      sel: params1.sel,
    }
  });
};
